package com.ebelter.btcomlib.utils;


import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Build;
import android.support.v4.content.FileProvider;
import android.text.TextUtils;
import android.util.Log;

import com.ebelter.btcomlib.utils.log.LogUtils;

import java.io.File;
import java.util.Locale;

public class AppUtils {

    private static final String TAG = "AppUtils";

    /**
     * 获取当前本地apk的版本
     */
    public static int getVersionCode(Context mContext) {
        int versionCode = 0;
        try {
            //获取软件版本号，对应AndroidManifest.xml下android:versionCode
            versionCode = mContext.getPackageManager().
                    getPackageInfo(mContext.getPackageName(), 0).versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionCode;
    }



    /**
     * 获取版本号名称
     */
    public static String getVerName(Context context) {
        String verName = "";
        try {
            verName = "V"+context.getPackageManager().
                    getPackageInfo(context.getPackageName(), 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return verName;
    }






    public static final  String zh="zh";//中文
    public static final  String en="en"; //英文
    public static final  String ko="ko"; //韩语
    public static final  String fr="fr"; //法语
    public static final  String de="de"; //德语
    public static final  String ja="ja"; //日语
    public static final  String ru="ru"; //俄语
    public static final  String iw="iw"; //希伯来语
    public static final  String il="il"; //爱尔兰

    public static String getLocalStr(){
//        获取系统语言 方法：Locale.getDefault().getLanguage()
//        获取应用语言 方法：getResources().getConfiguration().locale.getLanguage()

        String systemLanguage = Locale.getDefault().getLanguage();
     //   String appLanguage = ApplicationHelper.mApplicationContecx.getResources().getConfiguration().locale.getLanguage();
      //  ULog.i(TAG,"---systemLanguage="+systemLanguage+"----appLanguage="+appLanguage);
        if (TextUtils.isEmpty(systemLanguage)){
            return zh;
        }
        systemLanguage=systemLanguage.toLowerCase();
        if (systemLanguage.contains(zh)){
            return zh;
        }else if (systemLanguage.contains(ko)){
            return ko;
        }else if (systemLanguage.contains(fr)){
            return fr;
        }else if (systemLanguage.contains(de)){
            return de;
        }else if (systemLanguage.contains(ja)){
            return ja;
        }else if (systemLanguage.contains(ru)){
            return ru;
        }else if(systemLanguage.contains(iw)||systemLanguage.contains(il)){//有的手机
            return iw;
        }else {
            return en;
        }

    }

    public static int getApkCode(Context context, String apkPath) {
        if (context==null)return -1;
        PackageManager pm = context.getPackageManager();
        PackageInfo info = pm.getPackageArchiveInfo(apkPath, PackageManager.GET_ACTIVITIES);
        if (info!=null){
              return info.versionCode;
        }
         return -1;
    }


    public static void installApp(Context pContext, String appFile,String authority){
        if (null == appFile)
            return;
        File pFile=new File(appFile);
        if (!pFile.exists())
            return;
        Intent _Intent = new Intent();
        _Intent.setAction(Intent.ACTION_VIEW);
        Uri _uri;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            _Intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            _uri = FileProvider.getUriForFile(pContext,  authority, pFile);
        }else {
            _uri = Uri.fromFile(pFile);
            _Intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        _Intent.setDataAndType(_uri, "application/vnd.android.package-archive");
        pContext.startActivity(_Intent);

    }

    public static boolean isLocationOpen(final Context context){
        LocationManager manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        //gps定位
        boolean isGpsProvider = false;
        boolean isNetWorkProvider=false;
        if (manager != null) {
            isGpsProvider = manager.isProviderEnabled(LocationManager.GPS_PROVIDER);
            //网络定位
            isNetWorkProvider = manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        }

        LogUtils.i(TAG,"----------isGpsProvider="+isGpsProvider+"------isNetWorkProvider="+isNetWorkProvider);
        return isGpsProvider|| isNetWorkProvider;
    }


}
